home *** CD-ROM | disk | FTP | other *** search
/ Libris Britannia 4 / science library(b).zip / science library(b) / COMMUNIC / TERMINAL / 1589.ZIP / TERMIO.C < prev    next >
Text File  |  1989-04-13  |  13KB  |  436 lines

  1.  
  2. #include "windows.h"
  3. #include <stdio.h>
  4. #include <fcntl.h>
  5. #include <stdarg.h>
  6. #include "icu_user.h"
  7. #include "term.h"
  8. #include "termdefs.h"
  9.  
  10. /* */
  11. /*doc************************************************************************
  12. *
  13. *     NAME
  14. *
  15. *     ReadLine() - read a line from the port buffer
  16. *
  17. *     FORMAT
  18. *
  19. *     ReadLine()
  20. *
  21. *     DESCRIPTION
  22. *     This function read the next line of data from the input queue. It
  23. *     will read until the port is empty or it has read 240 characters.
  24. *     This line will get put into the 'next_line' field. If it exceeds the
  25. *     width of the screen, the 'next_line' variable will be incremented
  26. *     and so on.
  27. *
  28. *     There is special character handling in this function to allow
  29. *     special characters and escape sequences to be handled.
  30. *
  31. *     RETURN VALUE
  32. *     TRUE - error reading data
  33. *     FALSE - no error
  34. *
  35. *     MODIFIES
  36. *     next_line
  37. *     top_line
  38. *
  39. *     REFERENCES
  40. *
  41. *end************************************************************************/
  42. ReadLine( )
  43. {
  44. /****************************************
  45. * local variable declarations           *
  46. ****************************************/
  47. char  port_char;                         /* character read from port         */
  48. int   chars_read;                        /* number of characters read        */
  49. int   read_stat;
  50.  
  51. /****************************************
  52. * start of function code                *
  53. ****************************************/
  54. chars_read = 240;
  55. read_stat  = 1;
  56.  
  57. TermShowCaret( FALSE );                      /* Hide the caret               */
  58.  
  59. do {
  60.    read_stat = ReadComm(nCID,(LPSTR)&port_char,1);/* Read the next character */
  61.    if ( read_stat < 1 )
  62.        break;                                /* Input buffer empty           */
  63.  
  64.    if ( Capture == TRUE )
  65.        CaptureLog( port_char );              /* Log every character read     */
  66.  
  67.    if (port_char == 127) {                   /* Destructive backspace        */
  68.        if (curs_pos.x > char_w)
  69.            curs_pos.x -= char_w;
  70.        if (next_char > 0)
  71.            next_char--;
  72.        display[next_line].string[next_char] = ' ';
  73.        *buff = ' ';
  74.        TextOut(hTermDC,curs_pos.x,curs_pos.y,(LPSTR) buff, 1); /* Erase char */
  75.        }
  76.    else if (port_char == ESC)          /* An ESCAPE sequence is starting     */
  77.        ProcessEscape( );
  78.    else if (port_char < 0x20)          /* A CONTROL character was sent       */
  79.        ProcessCtrl(port_char);
  80.    else {                              /* if NORMAL character, put on screen */
  81.        if ( GraphicsMode )
  82.            port_char = TranslateGraphic( port_char );
  83.  
  84.        TextOut( hTermDC, curs_pos.x, curs_pos.y, (LPSTR) &port_char, 1 );
  85.        display[next_line].string[next_char] = port_char;
  86.        UpdateCursPos(NEXT_CHAR);       /* Move cursor to next character     */
  87.        }                               /* End if normal character           */
  88.    } while ( --chars_read );           /* Go until EOL or read 80 chars     */
  89.  
  90. OUT:
  91. TermShowCaret( TRUE );                 /* Display the caret               */
  92. return(FALSE);
  93. }
  94.  
  95. /* */
  96. /*doc************************************************************************
  97. *
  98. *     NAME
  99. *
  100. *     UpdateCursPos() - update cursor position on screen
  101. *
  102. *     FORMAT
  103. *
  104. *     UpdateCursPos(how)
  105. *     int how  -  how to update position (NEXT_LINE, NEXT_CHAR or DOWN_LINE)
  106. *
  107. *     DESCRIPTION
  108. *     This function will advance the cursor. How it is advanced depends on
  109. *     the 'how' flag. If it is NEXT_LINE, the cursor will be moved to the
  110. *     start of the next line, and the screen will be scrolled if necessary.
  111. *     If it is NEXT_CHAR, the cursor will be moved over once to the right
  112. *     unless at the edge of the screen, when it will be moved to the next
  113. *     line.  The cursor will be moved down one line if how is DOWN_LINE.
  114. *
  115. *     RETURN VALUE
  116. *
  117. *     MODIFIES
  118. *     curs_pos
  119. *
  120. *     REFERENCES
  121. *
  122. *end************************************************************************/
  123. UpdateCursPos(how)
  124. int how;
  125. {
  126. /****************************************
  127. * local variable declarations           *
  128. ****************************************/
  129. int   i;
  130. char  *hold;
  131. /****************************************
  132. * if cursor is at edge of screen, move  *
  133. *  it to the next line                  *
  134. ****************************************/
  135. if ((how == NEXT_CHAR)&&((curs_pos.x/char_w)>=no_cols||next_char>=no_cols))
  136.    how = NEXT_LINE;
  137. /****************************************
  138. * handle moving the cursor to the next  *
  139. *  character position                   *
  140. ****************************************/
  141. if (how == NEXT_CHAR) {
  142.    curs_pos.x += char_w;
  143.    next_char++;
  144.    }
  145.  
  146. /****************************************
  147. * handle moving the cursor to the next  *
  148. *  line and scrolling the screen        *
  149. ****************************************/
  150. else {  /* NEXT_LINE or DOWN_LINE */
  151.    if ( how != DOWN_LINE ) {
  152.    curs_pos.x = char_w;                 /* will start on new line    */
  153.    next_char  = 0;
  154.    }                                    /* Is screen full???         */
  155.    if ( next_line >= no_lines || curs_pos.y/char_h >= no_lines) {
  156.        hold = display[0].string;            /* Scroll display memory     */
  157.        for( i = 0; i < (no_lines-1); i++ )
  158.            display[i].string = display[i+1].string;
  159.        memset( hold, ' ', no_cols );
  160.        display[no_lines-1].string = hold;
  161.        next_line = no_lines - 1;
  162.        PaintWindow = FALSE;
  163.        ScrollWindow(hWndTerm,0,0 - char_h,NULL,NULL); /* Scroll whole window */
  164.        UpdateWindow(hWndTerm);
  165.        }
  166.    else {                               /* if screen isn't full             */
  167.       curs_pos.y += char_h;
  168.       next_line++;
  169.       }
  170.    }
  171.  
  172. return(FALSE);
  173. }
  174.  
  175. /* */
  176. /*doc************************************************************************
  177. *
  178. *     NAME
  179. *
  180. *     TranslateKey() - translate certain keys into control sequences
  181. *
  182. *     FORMAT
  183. *
  184. *     TranslateKey(key)
  185. *     int key  -  windows name for the key to be translated
  186. *
  187. *     DESCRIPTION
  188. *     This function translates the key passed to it into a sequence of
  189. *     control and ascii characters. It sends those characters to the
  190. *     computer. If the key passed to it is not one processed here, it
  191. *     returns a FALSE, if it is processed, it returns a TRUE.
  192. *
  193. *     RETURN VALUE
  194. *     TRUE - key processed
  195. *     FALSE - key not processed
  196. *
  197. *     MODIFIES
  198. *
  199. *     REFERENCES
  200. *     TransmitCommChar()
  201. *
  202. *end************************************************************************/
  203. TranslateKey(key)
  204. int key;
  205. {
  206. /****************************************
  207. * local variable declarations           *
  208. ****************************************/
  209. int  i, ret, sendindex ;
  210. sendindex = -1;
  211.  
  212. switch (key) {
  213.    case VK_F1:                          /* an F1 function key was pressed   */
  214.        if ( HIWORD(GetKeyState(VK_SHIFT)) )
  215.            sendindex = FUNCTION11;
  216.        else
  217.            sendindex = FUNCTION1;
  218.        break;
  219.    case VK_F2:                          /* an F2 function key was pressed   */
  220.        if ( HIWORD(GetKeyState(VK_SHIFT)) )
  221.            sendindex = FUNCTION12;
  222.        else
  223.            sendindex = FUNCTION2;
  224.        break;
  225.    case VK_F3:                          /* an F3 function key was pressed   */
  226.        if ( HIWORD(GetKeyState(VK_SHIFT)) )
  227.            sendindex = FUNCTION13;
  228.        else
  229.            sendindex = FUNCTION3;
  230.        break;
  231.    case VK_F4:                          /* an F4 function key was pressed   */
  232.        if ( HIWORD(GetKeyState(VK_SHIFT)) )
  233.            sendindex = FUNCTION14;
  234.        else
  235.            sendindex = FUNCTION4;
  236.        break;
  237.    case VK_F5:                          /* an F5 function key was pressed   */
  238.        if ( HIWORD(GetKeyState(VK_SHIFT)) )
  239.            sendindex = FUNCTION15;
  240.        else
  241.            sendindex = FUNCTION5;
  242.        break;
  243.    case VK_F6:                          /* an F6 function key was pressed   */
  244.        if ( HIWORD(GetKeyState(VK_SHIFT)) )
  245.            sendindex = FUNCTION16;
  246.        else
  247.            sendindex = FUNCTION6;
  248.        break;
  249.    case VK_F7:                          /* an F7 function key was pressed   */
  250.        sendindex = FUNCTION7;
  251.        break;
  252.    case VK_F8:                          /* an F8 function key was pressed   */
  253.        sendindex = FUNCTION8;
  254.        break;
  255.    case VK_F9:                          /* an F9 function key was pressed   */
  256.        sendindex = FUNCTION9;
  257.        break;
  258.    case VK_F10:                         /* an F10 function key was pressed  */
  259.        sendindex = FUNCTION0;
  260.        break;
  261.    case VK_F11:
  262.        sendindex = FUNCTION11;
  263.        break;
  264.    case VK_F12:
  265.        sendindex = FUNCTION12;
  266.        break;
  267.    case VK_F13:
  268.        sendindex = FUNCTION13;
  269.        break;
  270.    case VK_F14:
  271.        sendindex = FUNCTION14;
  272.        break;
  273.    case VK_F15:
  274.        sendindex = FUNCTION15;
  275.        break;
  276.    case VK_F16:
  277.        sendindex = FUNCTION16;
  278.        break;
  279.    case VK_LEFT:                        /* left arrow key */
  280.        sendindex = LEFTARROW;
  281.        break;
  282.    case VK_UP:                          /* up arrow key */
  283.        sendindex = UPARROW;
  284.        break;
  285.    case VK_RIGHT:                       /* right arrow key */
  286.        sendindex = RIGHTARROW;
  287.        break;
  288.    case VK_DOWN:                        /* down arrow key */
  289.        sendindex = DOWNARROW;
  290.        break;
  291.    default:
  292.        return (FALSE);                   /* this key wasn't processed        */
  293.        break;
  294.       }
  295.  
  296. if ( sendindex != -1 ) {            /* Send character sequence */
  297.    for ( i = 0; emulate[i].meaning != sendindex && emulate[i].meaning; i++ );
  298.    ret = strlen( emulate[i].sequence );
  299.    if ( ret > 0 )
  300.        ret = WriteComm( nCID, (LPSTR) emulate[i].sequence, ret );
  301.    }
  302.  
  303. return(TRUE);                           /* the key was processed            */
  304. }
  305.  
  306. /****************************************************************************/
  307. /****************************************************************************/
  308. OpenCapture()
  309. {
  310. if ( CaptureFD > 0 )
  311.    return( 0 );
  312. LoadString( hInstTerm, IDSCAPTURE, (PSTR) buff, 30 );
  313. CaptureFD = open( buff, O_CREAT|O_TRUNC|O_WRONLY|O_BINARY, 0000200 );
  314. if ( CaptureFD < 1 )
  315.    return( -1 );
  316.  
  317. return( TRUE );
  318. }
  319.  
  320. /****************************************************************************/
  321. /****************************************************************************/
  322. CloseCapture()
  323. {
  324. Capturelog( (char) -1 );    /* Flush the log file */
  325. close( CaptureFD );
  326. }
  327.  
  328. /****************************************************************************/
  329. /****************************************************************************/
  330. CaptureLog( port_char )
  331. char port_char;
  332. {
  333. int ret;
  334. static int cnt = 0;
  335.  
  336. if ( port_char == (char) -1 || port_char == 0x0A || cnt > 65 ) {
  337.    if ( port_char != 0x0A )
  338.        CaptureBuff[cnt++] = port_char;
  339.    CaptureBuff[cnt++] =  ']';
  340.    CaptureBuff[cnt++] = '\n';
  341.    CaptureBuff[cnt]   = '\0';
  342.    ret = write( CaptureFD, CaptureBuff, cnt );
  343.    memset( CaptureBuff, '\0', 80 );
  344.    sprintf( CaptureBuff, "%4d [", CaptureLine++ );
  345.    cnt = 0;
  346.    }
  347. else
  348.    CaptureBuff[cnt++] = port_char;
  349. }
  350.  
  351.  
  352.  
  353. /****************************************************************************/
  354. /****************************************************************************/
  355. TranslateGraphic( GraphicNumber )
  356. char  GraphicNumber;
  357. {
  358. char  Graphic;
  359.  
  360. switch( GraphicNumber ) {
  361.     case '0':
  362.        Graphic = 194;  /* ┬ */
  363.        break;
  364.     case '1':
  365.        Graphic = 192;  /* └ */
  366.        break;
  367.     case '2':
  368.        Graphic = 218;  /* ┌ */
  369.        break;
  370.     case '3':
  371.        Graphic = 191;  /* ┐ */
  372.        break;
  373.     case '4':
  374.        Graphic = 195;  /* ├ */
  375.        break;
  376.     case '5':
  377.        Graphic = 217;  /* ┘ */
  378.        break;
  379.     case '6':
  380.        Graphic = 179;  /* │ */
  381.        break;
  382.     case '7':
  383.        Graphic = 178;  /* ▓ */
  384.        break;
  385.     case '8':
  386.        Graphic = 197;  /* ┼ */
  387.        break;
  388.     case '9':
  389.        Graphic = 180;  /* ┤ */
  390.        break;
  391.     case ':':
  392.        Graphic = 196;  /* ─ */
  393.        break;
  394.     case ';':
  395.        Graphic = 177;  /* ▒ */
  396.        break;
  397.     case '<':
  398.        Graphic = 205;  /* ═ */
  399.        break;
  400.     case '=':
  401.        Graphic = 193;  /* ┴ */
  402.        break;
  403.     case '>':
  404.        Graphic = 186;  /* ║ */
  405.        break;
  406.     case '?':
  407.        Graphic = 176;  /* ░ */
  408.        break;
  409.     default:
  410.        Graphic = GraphicNumber;
  411.        break;
  412.     }
  413. return( Graphic );
  414. }
  415.  
  416. /****************************************************************************/
  417. /****************************************************************************/
  418. Beep()
  419. {
  420. MessageBeep( 1 );
  421. }
  422.  
  423. /****************************************************************************/
  424. /****************************************************************************/
  425. TermShowCaret( mode )
  426. int mode ;
  427. {
  428. if ( TermHasFocus && mode && CaretMode ) {
  429.    SetCaretPos( curs_pos.x, curs_pos.y );
  430.    ShowCaret( hWndTerm );
  431.    }
  432. else if ( TermHasFocus )
  433.    HideCaret( hWndTerm );
  434. }
  435.  
  436.